package com.zhijiaoyun.service.impl;

import com.zhijiaoyun.common.StringUtils;
import com.zhijiaoyun.common.Constant;
import com.zhijiaoyun.common.DsConstant;
import com.zhijiaoyun.common.ResponseUtils;
import com.zhijiaoyun.dao.INewsDao;
import com.zhijiaoyun.dao.INewsRecommendDao;
import com.zhijiaoyun.model.News;
import com.zhijiaoyun.model.NewsRecommend;
import com.zhijiaoyun.model.dto.NewsDto;
import com.zhijiaoyun.model.dto.PageData;
import com.zhijiaoyun.model.dto.PageList;
import com.zhijiaoyun.service.INewsRecommendService;
import com.zhijiaoyun.service.INewsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author shisfish
 * @date 2017/6/6
 */
@Service(value = "newsService")
public class NewsServiceImpl implements INewsService {

    @Resource
    INewsDao newsDao;

    @Resource
    INewsRecommendDao newsRecommendDao;

    @Resource
    INewsRecommendService newsRecommendService;

    @Override
    public News getNewsByTitle(String title) {
        return newsDao.selectBytitle(title);
    }

    @Override
    public void addNews(News news) {
        newsDao.insert(news);
    }

    @Override
    public News getNewsByNewsId(Long newsId) {
        return newsDao.selectByPrimaryKey(newsId);
    }

    @Override
    public News getNewsByTitleNotNewsId(Long newsId, String title) {
        return newsDao.selectByTitleNotNewsId(newsId, title);
    }

    @Override
    public void modifyNews(News news) {
        newsDao.updateByPrimaryKey(news);
    }

    @Override
    public PageList<NewsDto> pageByParamsDto(Integer pageIndex, Integer pageSize, String title, List<Long> ids) {
        int count = newsDao.countByParams(title, ids);
        PageData pageData = new PageData();
        List<NewsDto> listD = new ArrayList<>();
        PageList<NewsDto> pageList = new PageList<>();
        if (count > 0) {
            pageData = PageData.createPage(count, pageIndex, pageSize);
            List<News> list = newsDao.pageByParams(pageData.getBeginIndex(), pageData.getPageSize(), title, ids);
            if (!StringUtils.isEmptyList(list)) {
                for (News news : list) {
                    NewsDto newsDto = news2NewsDto(news);

                    listD.add(newsDto);
                }
            }
            pageList = new PageList<>(listD, pageData);
        }
        return pageList;
    }

    @Override
    public NewsDto getNewsDtoByNewsId(Long newsId) {
        News news = newsDao.selectByPrimaryKey(newsId);
        NewsDto newsDto = null;
        if (news != null) {
            newsDto = news2NewsDto(news);
        }
        return newsDto;
    }

    private NewsDto news2NewsDto(News news) {
        NewsDto newsDto = new NewsDto(news);
        // 是否推荐
        NewsRecommend newsRecommend = newsRecommendDao.selectByNewsId(news.getNewsId());
        if (newsRecommend == null) {
            newsDto.setIsRecommend(false);
        } else {
            newsDto.setIsRecommend(true);
        }

        return newsDto;
    }

    @Override
    public PageList<NewsDto> pageByParamsDtoHot(Integer pageIndex, Integer pageSize, String string, ArrayList<Long> ids) {
        int count = newsDao.countByParamsHot("", ids);
        PageData pageData = new PageData();
        List<NewsDto> listD = new ArrayList<>();
        PageList<NewsDto> pageList = new PageList<>();
        if (count > 0) {
            pageData = PageData.createPage(count, pageIndex, pageSize);
            List<News> list = newsDao.pageByParamsHot(pageData.getBeginIndex(), pageData.getPageSize(), "", ids);
            if (!StringUtils.isEmptyList(list)) {
                for (News news : list) {
                    NewsDto newsDto = news2NewsDto(news);

                    listD.add(newsDto);
                }
            }
            pageList = new PageList<>(listD, pageData);
        }
        return pageList;
    }

    @Override
    public void addViewNum(Long newsId) {
        newsDao.addViewNum(newsId);
    }

    @Override
    public void addPraiseNum(Long newsId) {
        newsDao.addPraiseNum(newsId);
    }

    @Override
    public void addCommentNum(Long newsId) {
        newsDao.addCommentNum(newsId);
    }

    @Override
    public void deleteNews(News news) {
        newsDao.deleteByPrimaryKey(news.getNewsId());
        // 删除推荐
        newsRecommendDao.deleteByNewsId(news.getNewsId());
    }

    @Override
    public Map<String, Object> deleteNewsById(long newsId) {
        News news = newsDao.selectByPrimaryKey(newsId);
        if (news == null) {
            return ResponseUtils.responseErrorMap(DsConstant.CODE_NEWS_LOST, DsConstant.MESSAGE_NEWS_LOST);
        }
        newsDao.deleteByPrimaryKey(news.getNewsId());
        // 删除推荐
        newsRecommendService.deleteByNewsId(news.getNewsId());
        return ResponseUtils.responseSuccessMap();
    }

    @Override
    public int countAll() {
        return newsDao.countAll();
    }

    @Override
    public Map<String, Object> pageNews(int pageIndex, int pageSize, String title) {
        PageList<NewsDto> newsPages = pageByParamsDto(pageIndex, pageSize, title, new ArrayList<>());

        Map<String, Object> map = new HashMap<>();
        map.put("list", newsPages.getList());
        map.put("page", newsPages.getPageData());
        return ResponseUtils.responseSuccessMap(map);
    }

    @Override
    public Map<String, Object> getNewsById(Long id) {
        NewsDto newsDto = getNewsDtoByNewsId(id);
        Map<String, Object> map = new HashMap<>();
        map.put("obj", newsDto);
        return ResponseUtils.responseSuccessMap(map);
    }

    @Override
    public Map<String, Object> insert(String title, String summary, String keyWord, String pictureStr, String source, String author, String content, HttpServletRequest request) {
        String createUser = String.valueOf(request.getAttribute(Constant.CREATE_USER));
        News news = new News("", title, summary, keyWord, pictureStr, source, author, new Date(), content, createUser);
        addNews(news);
        return ResponseUtils.responseSuccessMap();
    }

    @Override
    public Map<String, Object> modify(News news, String title, String keyWord, String pictureStr, String source, String author, String summary, String content, HttpServletRequest request) {
        String createUser = String.valueOf(request.getAttribute(Constant.CREATE_USER));
        news.setTitle(title);
        if (!StringUtils.isEmptyString(keyWord)) {
            news.setKeyWord(keyWord);
        }
        if (!StringUtils.isEmptyString(source)) {
            news.setSource(source);
        }
        if (!StringUtils.isEmptyString(author)) {
            news.setAuthor(author);
        }
        if (!StringUtils.isEmptyString(summary)) {
            news.setSummary(summary);
        }
        if (!StringUtils.isEmptyString(content)) {
            news.setContent(content);
        }
        if (!StringUtils.isEmptyString(pictureStr)) {
            news.setImageUrl(pictureStr);
        }
        news.setModifyTime(new Date());
        news.setModifyUser(createUser);
        modifyNews(news);

        return ResponseUtils.responseSuccessMap();
    }

    @Override
    public Map<String, Object> pageNewsHot(int pageIndex, int pageSize) {
        PageList<NewsDto> news = pageByParamsDtoHot(pageIndex, pageSize, "", new ArrayList<>());
        Map<String, Object> map = new HashMap<>();
        map.put("list", news.getList());
        map.put("page", news.getPageData());

        return ResponseUtils.responseSuccessMap(map);
    }

}
